
var Y = {}; // Global object, contains YUI modules
var V = {}; // Global object, contains any variables, functions, etc which user wants

var Loader = {
	pre_show: [],
	to_run: [],
	lauched: false,
	progress: 0,
	progress_bar: false,
	ViewPort: {},
	content_div_width: 0,

	start_loading: function() {
		this.progress_bar = document.getElementById("progress_bar");
		this.inc_progress(0);
		this.load_libraries();
	},

	inc_progress: function(inc_value) {
		this.progress += inc_value;
		if (this.progress > 100) {
			this.progress = 100;
		}
		this.progress_bar.style.width = (this.progress * 3) + "px";

		if (this.progress > 99 && !this.lauched) {
			this.lauched = true;
			Y.Event.on("domready", Loader.start_app, Loader);
		}
	},

	load_libraries: function() {
		var percents_per_function = 100 / 3;
		YUI().use("event", function(Obj) { Y.Event = Obj; Loader.inc_progress(percents_per_function); });
		YUI().use("node", function(Obj) { Y.Node = Obj; Loader.inc_progress(percents_per_function); });
		YUI().use("anim", function(Obj) { Y.Anim = Obj; Loader.inc_progress(percents_per_function); });
	},

	onReady: function(func) {
		this.to_run.push(func);
	},

	preShow: function(func) {
		this.pre_show.push(func);
	},

	normalize_pages: function() {

		var global = Y.Node.one(".global");

		Y.Node.one("#loading_div").setStyle("display", "none");
		Y.Node.one("#content").setStyle("display", "block");

		//this.ViewPort.width = global.get("winWidth") - parseInt(global.getStyle("marginLeft"));
		//this.ViewPort.height = global.get("winHeight") - parseInt(global.getStyle("marginTop")) - 10;
		this.ViewPort.width = 1326;
		this.ViewPort.height = 538;

		global.setStyle("width", this.ViewPort.width + "px");
		global.setStyle("height", this.ViewPort.height + "px");

		Y.Node.all(".page_div").each(function() {
			this.setStyle("width", Loader.ViewPort.width + "px");
			this.setStyle("height", Loader.ViewPort.height + "px");

			Loader.content_div_width += Loader.ViewPort.width;
		});

		V.Content = Y.Node.one("#content");
		V.Content.setStyle("width", this.content_div_width + "px");
	},

	start_app: function() {
		var i, func;

		for (i = 0; i < this.pre_show.length; i++) {
			func = this.pre_show[i];
			func();
		}

		this.normalize_pages();

		for (i = 0; i < this.to_run.length; i++) {
			func = this.to_run[i];
			func();
		}
	}
};
Loader.start_loading();

var Util = {
	replaceNodeClass: function(node, class1, class2) {
		node.hasClass(class1) ? node.replaceClass(class1, class2) : node.replaceClass(class2, class1);
	},

	set_def_values: function(elements_to_set) {

		var i, element;

		for (i = 0; i < elements_to_set.length; i++) {

			element = elements_to_set[i];

			if (!element.id || !element.def_text)
				continue;

			var node = Y.Node.one("#" + element.id);
			if (!node) return;

			if (!element.prop) {
				var node_name = node.get("nodeName").toLowerCase();
				if (node_name === "input" || node_name === "textarea") {
					element.prop = "value";
				} else {
					return;
				}
			}

			element.active_color = element.active_color || "black";
			element.inactive_color = element.inactive_color || "grey";

			// adding event-handler on keypress event
			Y.Node.one("#" + element.id).on("keypress", function(e, def_text, prop, active_color) {
				if (this.get(prop) == def_text) {
					this.set(prop, "");
					this.setStyle("color", active_color);
				}
			}, null, element.def_text, element.prop, element.active_color);

			// adding event-handler on key event
			Y.Node.one("#" + element.id).on("focus", function(e, def_text, prop, active_color) {
				if (this.get(prop) == def_text) {
					this.set("selectionStart", 0);
					this.set("selectionEnd", 0);
				}
			}, null, element.def_text, element.prop, element.active_color);

			// adding event-handler on blur event
			Y.Node.one("#" + element.id).on("blur", function(e, def_text, prop, inactive_color) {
				if (this.get(prop) == "") {
					this.set(prop, def_text);
					this.setStyle("color", inactive_color);
				}
			}, null, element.def_text, element.prop, element.inactive_color);

			// setting default values
			if (node.get(element.prop) == "" || node.get(element.prop) == element.def_text) {
				node.set(element.prop, element.def_text);
				node.setStyle("color", element.inactive_color);
				node.set("selectionStart", 0);
				node.set("selectionEnd", 0);
			}
		}
	}
};
